package com.ooqiu.gaming.api;


import com.ooqiu.gaming.common.Constants;
import com.ooqiu.gaming.components.retrofit.ConvertData;
import com.ooqiu.gaming.components.retrofit.convert.FastJsonConverterFactory;
import com.ooqiu.gaming.injector.PerActivity;
import com.ooqiu.gaming.ui.feedback.UploadRes;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by Eenie on 2017/6/7.
 * Email: eeniework@gmail.com
 * Des:
 */

@PerActivity
public class FileManager {

    private FileService mForumService;

    @Inject
    public FileManager(OkHttpClient client) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.HOST)
                .client(client)
                .addConverterFactory(FastJsonConverterFactory.create(null))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        mForumService = retrofit.create(FileService.class);
    }


    public Observable<UploadRes> uploadImg(String path) {
        MultipartBody.Part body = null;
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        body = MultipartBody.Part.createFormData("image", file.getName(), requestFile);
        return mForumService.uploadImage(body)
                .map(new ConvertData<UploadRes>(UploadRes.class));
    }






}
